<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Model\Commodity;
use App\Model\Commodity_color;
use App\Model\Commodity_info;
use App\Model\Commodity_size;
use App\Model\Order;
use App\Model\Parent_order;
use App\Model\User_address;
use Illuminate\Http\Request;

/**
 * Class OrderController
 * @package App\Http\Controllers\User
 * @User wuqin
 * @Date 2018/4/12 0012
 * @Time 20:50
 */
class OrderController extends Controller
{
    /**
     * @var Order
     */
    private $order;
    /**
     * @var Parent_order
     */
    private $parent_order;
    /**
     * @var User_address
     */
    private $user_address;
    /**
     * @var \Illuminate\Config\Repository|mixed
     */
    private $state_back;

    /**
     * OrderController constructor.
     * @param Order $order
     * @param Parent_order $parent_order
     * @param Request $request
     * @param User_address $user_address
     */
    function __construct(Order $order, Parent_order $parent_order, Request $request, User_address $user_address)

    {
        $this->order = $order;
        $this->parent_order = $parent_order;
        $this->user_address = $user_address;
        $this->state_back = config("bar.parent_order.state_back");
    }

    /**
     * 方法 : index
     * @User wuqin
     * @DateTime 2018-04-12 20:50
     * @param Request $request
     * @param int $orderId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request, int $orderId)
    {
        if (empty((int)$orderId)) {
            return $this->error_url('/', '非法订单', 3);
        }

        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        $data = array();

        $this->parent_order = $this->parent_order->find($orderId);
        if (empty($this->parent_order) || empty($this->parent_order->toArray()))
            return $this->error_url('/', '无效订单', 3);

        $data["parent_order"] = $this->parent_order->toArray();
        $data["parent_order"]["state_name"] = config("bar.parent_order.state")[$this->parent_order->state];
        $data["parent_order"]["state_name_back"] = $this->state_back[$this->parent_order->state];


        $this->user_address = new User_address();
        $this->user_address = $this->user_address->find($this->parent_order->user_address_id);
        if (empty($this->user_address) || empty($this->user_address->toArray()))
            return $this->error_url('/', '无效地址', 3);

        $data["parent_order"]["user_address"] = $this->user_address->toArray();
        $data["parent_order"]["user_address"]["phone"] = substr_replace($data["parent_order"]["user_address"]["phone"], "****", 3, 4);


        $this->order = new Order();
        $order = $this->order->getOrderByParent($this->parent_order->parent_order_id);
        if (empty($order))
            $data["parent_order"]["data"][] = $order;
        else {
            foreach ($order as $k => $v) {
                $CommodityModel = new Commodity();
                $colorModel = new Commodity_color();
                $sizeModel = new Commodity_size();
                $infoModel = new Commodity_info();
                $commodityRes = $CommodityModel->get_commodity_by_commidity_id($v["commodity_id"]);
                if (empty($commodityRes) || count($commodityRes) == 0) {
                    unset($order);
                    continue;
                }
                $v = array_merge($v, reset($commodityRes));
                $v = array_merge($v, $colorModel->get_redis('commodity_color:' . $v["color_id"] . ':commodity:' . $v["commodity_id"]));
                $v = array_merge($v, $sizeModel->get_redis('commodity_size:' . $v["size_id"] . ':commodity:' . $v["commodity_id"]));
                $v = array_merge($v, $infoModel->get_redis("commodity_info:" . $v["info_id"] . ":commodity:" . $v["commodity_id"] . ":color:" . $v["color_id"] . ":size:" . $v["size_id"]));
                unset($v["commodity_desc"]);
                $data["parent_order"]["data"][] = $v;
            }
        }
        $data["state"] = $this->state_back;
        return view("User.Order.index", $data);
    }

    /**
     * 方法 : list
     * @User wuqin
     * @DateTime 2018-04-12 20:50
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function list(Request $request)
    {
        $data = array();

        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        $parent_order = $this->parent_order->getOrderByUserId($user["user_id"]);
        if (!empty($parent_order)) {
            foreach ($parent_order as $key => $value) {
                $data["parent_order"][$key] = $value;
                $data["parent_order"][$key]["state_name"] = $this->state_back[$value["state"]];
//                $this->user_address = new User_address();
//                $this->user_address = $this->user_address->find($value["user_address_id"]);
//                $data["parent_order"][$key]["user_address"] = $this->user_address->toArray();

                $this->order = new Order();
                $order = $this->order->getOrderByParent($value["parent_order_id"]);
                if (empty($order))
                    $data["parent_order"][$key]["data"][] = $order;
                else {
                    foreach ($order as $k => $v) {
//                        $CommodityModel = new Commodity();
//                        $colorModel = new Commodity_color();
//                        $sizeModel = new Commodity_size();
                        $infoModel = new Commodity_info();
//                        $commodityRes = $CommodityModel->get_commodity_by_commidity_id($v["commodity_id"]);
//                        if (empty($commodityRes) || count($commodityRes) == 0) {
//                            unset($order[$key]);
//                            continue;
//                        }
//                        $v = array_merge($v, reset($commodityRes));
//                        $v = array_merge($v, $colorModel->get_redis('commodity_color:' . $v["color_id"] . ':commodity:' . $v["commodity_id"]));
//                        $v = array_merge($v, $sizeModel->get_redis('commodity_size:' . $v["size_id"] . ':commodity:' . $v["commodity_id"]));
                        $v = array_merge($v, $infoModel->get_redis("commodity_info:" . $v["info_id"] . ":commodity:" . $v["commodity_id"] . ":color:" . $v["color_id"] . ":size:" . $v["size_id"]));
                        unset($v["commodity_desc"]);
                        $data["parent_order"][$key]["data"][] = $v;
                    }
                }
            }
        } else {
            $data["parent_order"] = $parent_order;
        }
        $data["nav"] = config('bar.parent_order.nav');
        $data["state"] = $this->state_back;
        $data["active"] = "000";
        return view("User.Order.list", $data);
    }

    /**
     * 方法 : zero
     * @User wuqin
     * @DateTime 2018-04-12 20:50
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function zero()
    {
        $data = array();

        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        $data = $this->getOrder($user["user_id"], 0);

        return view("User.Order.list", $data);
    }

    /**
     * 方法 : one
     * @User wuqin
     * @DateTime 2018-04-12 20:50
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function one()
    {

        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        $data = $this->getOrder($user["user_id"], 1);

        return view("User.Order.list", $data);
    }

    /**
     * 方法 : two
     * @User wuqin
     * @DateTime 2018-04-12 20:50
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function two()
    {

        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        $data = $this->getOrder($user["user_id"], 2);

        return view("User.Order.list", $data);
    }

    /**
     * 方法 : three
     * @User wuqin
     * @DateTime 2018-04-12 20:50
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function three()
    {

        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        $data = $this->getOrder($user["user_id"], 3);

        return view("User.Order.list", $data);
    }

    /**
     * 方法 : getOrder
     * @User wuqin
     * @DateTime 2018-04-12 20:50
     * @param int $user_id
     * @param int $number
     * @return array
     */
    private function getOrder(int $user_id, int $number)
    {
        $data = array();

        $parent_order = $this->parent_order->getOrderByUserIdAndState($user_id, $number);

        if (!empty($parent_order)) {
            foreach ($parent_order as $key => $value) {
                $data["parent_order"][$key] = $value;
                $data["parent_order"][$key]["state_name"] = $this->state_back[$value["state"]];
//                $this->user_address = new User_address();
//                $this->user_address = $this->user_address->find($value["user_address_id"]);
//                $data["parent_order"][$key]["user_address"] = $this->user_address->toArray();

                $this->order = new Order();
                $order = $this->order->getOrderByParent($value["parent_order_id"]);
                if (empty($order))
                    $data["parent_order"][$key]["data"][] = $order;
                else {
                    foreach ($order as $k => $v) {
//                        $CommodityModel = new Commodity();
//                        $colorModel = new Commodity_color();
//                        $sizeModel = new Commodity_size();
                        $infoModel = new Commodity_info();
//                        $commodityRes = $CommodityModel->get_commodity_by_commidity_id($v["commodity_id"]);
//                        if (empty($commodityRes) || count($commodityRes) == 0) {
//                            unset($order[$key]);
//                            continue;
//                        }
//                        $v = array_merge($v, reset($commodityRes));
//                        $v = array_merge($v, $colorModel->get_redis('commodity_color:' . $v["color_id"] . ':commodity:' . $v["commodity_id"]));
//                        $v = array_merge($v, $sizeModel->get_redis('commodity_size:' . $v["size_id"] . ':commodity:' . $v["commodity_id"]));
                        $v = array_merge($v, $infoModel->get_redis("commodity_info:" . $v["info_id"] . ":commodity:" . $v["commodity_id"] . ":color:" . $v["color_id"] . ":size:" . $v["size_id"]));
                        unset($v["commodity_desc"]);
                        $data["parent_order"][$key]["data"][] = $v;
                    }
                }
            }
        } else {
            $data["parent_order"] = $parent_order;
        }

        $data["nav"] = config('bar.parent_order.nav');

        $data["state"] = $this->state_back;
        $data["active"] = $number;
        return $data;
    }
}
